 <!-- tree -->
<template>
  <div class="tree">
    <el-input placeholder="搜索" v-model="filterText">
    </el-input>
    <el-tree ref="tree" highlight-current :style="`min-width: ${treeWidth}`" :data="data" :props="props"
      :expand-on-click-node="false" :filter-node-method="filterNode" :node-key="props.value"
      :default-expand-all="props.hasOwnProperty('defaultExpandAll') ? props.defaultExpandAll : true " @node-click="onClickNode" :current-node-key="value"
      :show-checkbox="props.showCheckbox || false" :default-checked-keys="checkedKeys" @check="handleCheckChange"
      :render-content="props.renderContent" :default-expanded-keys="props.defaultExpandedKeys">
    </el-tree>
  </div>
</template>

<script>
export default {
  name: 'Pagination',
  props: {
    // 接收绑定参数
    value: String | Number,
    // 输入框宽度
    width: String,
    // 选项数据
    options: {
      type: Array,
      required: true,
    },
    // 输入框占位符
    placeholder: {
      type: String,
      required: false,
      default: '请选择',
    },
    // 树节点配置选项
    props: {
      type: Object,
      required: false,
      default: () => ({
        parent: 'parentId',
        value: 'rowGuid',
        label: 'areaName',
        children: 'children',
        defaultExpandAll: true
      }),
    },
    checkedKeys: {
      type: Array,
      default: () => ([])
    }
  },
  // 设置绑定参数
  model: {
    prop: 'value',
    event: 'selected',
  },
  computed: {
    // 是否为树状结构数据
    dataType() {
      const jsonStr = JSON.stringify(this.options);
      return jsonStr.indexOf(this.props.children) !== -1;
    },
    // 若非树状结构，则转化为树状结构数据
    data() {
      return this.dataType ? this.options : this.switchTree();
    },
  },
  watch: {
    value(val) {
      this.$nextTick(function () {
        this.$refs.tree.setCurrentKey(val)
      })
    },
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  data() {
    return {
      // 菜单宽度
      treeWidth: 'auto',
      // 实际请求传值
      valueModel: '0',
      filterText: '',
    };
  },
  created() {
    if (this.value) { }
  },
  methods: {
    // 单击节点
    onClickNode(node, data) {
      this.$emit('onClickNode', node, data)
    },
    // 复选
    handleCheckChange(data, checked) {
      this.$emit('checkChange', data, checked)
    },
    // 偏平数组转化为树状层级结构
    switchTree() {
      return this.cleanChildren(this.buildTree(this.options, '0'));
    },
    // 树节点过滤方法
    filterNode(query, data) {
      if (!query) return true;
      return data[this.props.label].indexOf(query) !== -1;
    },
    // 搜索树状数据中的 ID
    queryTree(tree, id) {
      let stark = [];
      stark = stark.concat(tree);
      while (stark.length) {
        const temp = stark.shift();
        if (temp[this.props.children]) {
          stark = stark.concat(temp[this.props.children]);
        }
        if (temp[this.props.value] === id) {
          return temp[this.props.label];
        }
      }
      return '';
    },
    // 将一维的扁平数组转换为多层级对象
    buildTree(data, id = '0') {
      const fa = (parentId) => {
        const temp = [];
        for (let i = 0; i < data.length; i++) {
          const n = data[i];
          if (n[this.props.parent] === parentId) {
            n.children = fa(n.rowGuid);
            temp.push(n);
          }
        }
        return temp;
      };
      return fa(id);
    },
    // 清除空 children项
    cleanChildren(data) {
      const fa = (list) => {
        list.map((e) => {
          if (e.children.length) {
            fa(e.children);
          } else {
            delete e.children;
          }
          return e;
        });
        return list;
      };
      return fa(data);
    },
  },
};
</script>

<style scoped lang="scss">
.tree {
  // height: 100%;
  padding: 10px;
  margin: 0 0 0 10px;
  height: calc(100vh - 220px);
}

.el-tree {
  height: calc(100% - 36px);
  overflow: auto;

  ::v-deep .el-tree-node>.el-tree-node__children {
    overflow: inherit;
  }
}

::v-deep .el-tree--highlight-current .el-tree-node.is-current>.el-tree-node__content {
  background-color: var(--button-bgc);
  color: #FFFFFF;
}

// .el-tree ::v-deep {
//   .el-tree-node__content {
//     position: relative;
//   }

//   //竖线
//   .el-tree-node:last-child>.el-tree-node__content::before {
//     content: "";
//     width: 1px;
//     height: 10000px;
//     border-width: 1px;
//     border-left: 1px solid #52627c;
//     position: absolute;
//     margin-left: -9px;
//     bottom: 13px;
//   }

//   //横线
//   .el-tree-node__children .el-tree-node__content::after {
//     content: "";
//     width: 13px;
//     height: 1px;
//     border-width: 1px;
//     border-top: 1px solid #52627c;
//     position: absolute;
//     margin-left: -9px;
//   }

//   .el-tree-node__content>.el-tree-node__expand-icon {
//     padding: 6px 3px;
//   }

//   .el-tree-node__expand-icon.expanded {
//     -webkit-transform: rotate(0deg);
//     transform: rotate(0deg);
//   }

//   //有子节点 且未展开
//   .el-icon-caret-right:before {
//     background: url("../../assets/images/tree_1.png") no-repeat 0;
//     content: "";
//     display: block;
//     width: 16px;
//     height: 16px;
//     font-size: 16px;
//     background-size: 16px;
//   }

//   //有子节点 且已展开
//   .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
//     background: url("../../assets/images/tree_2.png") no-repeat 0;
//     content: "";
//     display: block;
//     width: 16px;
//     height: 16px;
//     font-size: 16px;
//     background-size: 16px;
//   }

//   //没有子节点
//   .el-tree-node__expand-icon.is-leaf::before {
//     background: url("../../assets/images/tree_2.png") no-repeat 0;
//     content: "";
//     display: block;
//     width: 16px;
//     height: 16px;
//     font-size: 16px;
//     background-size: 16px;
//   }

//   /* 点击树结构项的选中颜色 */
//   .el-tree--highlight-current .is-current.el-tree-node>.el-tree-node__content {
//     background-color: #99ccff !important;
//   }
// }
</style>